/**
  ******************************************************************************
  * @file    GPIO/IOToggle/main.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main program body.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "stm32f10x.h"
//#include "stm32_eval.h"
#include "drivers.h"
#include "dds_com.h"
#include "dds_ctl.h"
#include "wave_generate.h"
//#include "sdio_sd.h"
//#include "stm32_eval_sdio_sd.h"
#include "diskio.h"
#include "hw_config.h" 
#include "usb_lib.h"
#include "usb_pwr.h"

/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup GPIO_IOToggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//GPIO
GPIO_InitTypeDef GPIO_InitStructure;
//ID Info
__IO uint32_t DeviceID = 0;
__IO uint32_t FlashID = 0;
/* Private function prototypes -----------------------------------------------*/
void NVIC_Configuration(void);
void UART_Config(void);
void LED_Config(void);
int SendChar(int ch);
int GetKey(void);
FATFS fs_disk;
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
	//unsigned long i=0;
	char conflag;
	
	/*Config system run at 72MHz*/
	SystemInit();
	
	/* Set the Vector Table base location at 0x3000 */ 
  //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000);
	//NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
	
	/*Config UART1 so that can use printf and scanf function*/
	UART_Config();
	
	/*Config LED to display system status*/
	LED_Config();
	
	//while(1)
	//USART_SendData(USART3,0XAA);
	//USART_SendData(EVAL_COM1, (uint8_t) ch);
	
	printf("\r\nSystem Init ... ");
	/* Interrupt Config */
	//Enable when use sd card
  //NVIC_Configuration();
	/*Delay Config*/
	delay_init(72);
	printf("OK!");
	
	/*sFlash Init*/
	printf("\r\nsFlash Init ... ");
	SPI_FLASH_Init();
	//SPI_FLASH_BulkErase();
	printf("OK!");
	SYS_LED_ON();
	/*--- Get SPI Flash  ID ---*/
	/*DeviceID = SPI_FLASH_ReadDeviceID();	
	delay_ms(200);
	FlashID = SPI_FLASH_ReadID();
	printf("\r FlashID is 0x%X,  Manufacturer Device ID is 0x%X\r\n", FlashID, DeviceID);*/
	/*--- Format Disk ---*/
	/*printf("\r\nformating ... ");
	if(f_mount(0,&fs_disk) == FR_OK)
	{
		printf("\r\nMount OK!\n");
		printf("\r\nformat result: %d \n",f_mkfs(0, 0, 0));
	}
	else
	{
		printf("Fail!\n");
	}*/
	
	/*RAM Disk Init*/
	/*printf("\r\nRAM Disk Init ... ");
	RAM_Disk_Init();	//clear RAM
	printf("\nFormating ... ");
	if(f_mount(1,&fs_disk) == FR_OK)
		printf("\nMount OK!\n");
	else
	{
		printf("Mount Fail!\n");
	}
	
	printf("result: %d \n",f_mkfs(1, 0, 0));*/
	
	
	/*FSMC Config*/
	printf("\r\nFSMC Init ... ");
	FSMC_FPGA_Init();
	printf("OK!");
	/*while(1)
	{
		fpgaWriteCMD(0x1111);
		fpgaWriteDAT(0xeeee);
	}*/
	SYS_LED_OFF();
	
	/*DAC Config*/
	printf("\r\nDAC Init ... ");
	//Internal_DacInit();
	SPI_DacInit();
	printf("OK!");
	SYS_LED_ON();
	/*while(1)
	{
		SPI_DACSendData(0x5f6,0x0f0);
		delay_us(5);
	}*/
	
	/*while(1)
	{
		//GPIO_ResetBits(GPIOB, GPIO_Pin_3);
		//I2S_DACSendData(0xffffff, 0xffffff);
		I2S_DACSendData(i, i+1);
		i = i + 2;
		//GPIO_SetBits(GPIOB, GPIO_Pin_3);
		//I2S_DACSendData(0x111111, 0x111111);
		I2S_DACSendData(i, i+1);
		i = i + 2;
		//delay_us(50);
		//delay_us(100);
	}*/
	
	/*FPGA Init*/
	printf("\r\nFPGA Init ... ");
	FPGA_Init();
	printf("OK!");
	/*while(1)
	{
		fpgaWriteCMD(0x0f0f);
		delay_us(1);
		fpgaWriteDAT(0xf0f0);
		delay_us(1);
	}*/
	SYS_LED_OFF();

	
	/*Generate Wave Data*/
	//waveGenerate_sin(512,8);
	//waveGenerate_tri(512,8);
	//waveGenerate_saw(512,8);
	//waveGenerate_sqa(512,8);

	
	/*Communiciate processing*/
	communicate_init();
	SYS_LED_OFF();
	
	/*USB Init*/
	printf("\r\nUSB Init ... ");
	Set_System();
	Set_USBClock();
	USB_Interrupts_Config();
	USB_Init();
	while (bDeviceState != CONFIGURED);
	printf("OK!\r\n");
	SYS_LED_ON();
	//SYS_LED_OFF();
	
	
	connect_est();
	
	//pacFileWrite();
	
	pacInit();
	
	while(1)
	{
		conflag = Connect_Detection();
		if(conflag == 1)
		{
			pacUpdate();
			
		}
		delay_ms(500);
	}
	

	
}

/**
  * @brief  Configures SDIO IRQ channel.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void UART_Config(void)
{
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;


	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	/*UART Init*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
	/* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* USART configuration */
  USART_Init(USART1, &USART_InitStructure);
    
  /* Enable USART */
  USART_Cmd(USART1, ENABLE);
// 	GPIO_InitTypeDef GPIO_InitStructure;
// 	USART_InitTypeDef USART_InitStructure;
// 	
// 	/* config USART1 clock */
// 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
// 	RCC_APB2PeriphClockCmd(/*RCC_APB2Periph_USART1 | */RCC_APB2Periph_GPIOC, ENABLE);
// 	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// 	
// 	GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);  

// 	/* USART1 GPIO config */
//    /* Configure USART1 Tx (PA.09) as alternate function push-pull */
//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//   GPIO_Init(GPIOC, &GPIO_InitStructure);    
//   /* Configure USART1 Rx (PA.10) as input floating */
//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
//   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//   GPIO_Init(GPIOC, &GPIO_InitStructure);
// 	  
// 	/* USART1 mode config */
// 	USART_InitStructure.USART_BaudRate = 115200;
// 	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// 	USART_InitStructure.USART_StopBits = USART_StopBits_1;
// 	USART_InitStructure.USART_Parity = USART_Parity_No ;
// 	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
// 	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// 	USART_Init(USART3, &USART_InitStructure); 
//   USART_Cmd(USART3, ENABLE);

}

void LED_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOC, GPIO_Pin_6);
}


int SendChar(int ch)
{
	USART_SendData(USART1, (unsigned char) ch);
  while (!(USART1->SR & USART_FLAG_TXE));
	
	return ch;
}

/**
  * @brief  Get a key from the HyperTerminal
  * @param  None
  * @retval The Key Pressed
  */
int GetKey(void)
{
  /* Waiting for user input */
 	while (!(USART1->SR & USART_FLAG_RXNE));
 	return ((int)USART_ReceiveData(USART1));

}

// int SendChar(int ch)
// {
// 	USART_SendData(USART3, (unsigned char) ch);
//   while (!(USART3->SR & USART_FLAG_TXE));
// 	
// 	return ch;
// }

// int GetKey(void)
// {
// 	while (!(USART3->SR & USART_FLAG_RXNE));
// 	return ((int)USART_ReceiveData(USART3));
// }

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
